package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowModelVariableVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowModelVariableRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowModelVariableDAO;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowModelVariable;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowModelVariableExample;
import org.flowable.engine.common.impl.cfg.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-06-17 16:39
 */
@Service
public class FlowModelVariableRepositoryImpl implements FlowModelVariableRepository {

    @Autowired
    private FlowModelVariableDAO flowModelVariableDAO;


    @Autowired
    private IdGenerator idGenerator;

    @Override
    public int insertModelVariable(FlowModelVariableVo vo) {
        vo.setId(idGenerator.getNextId());
        FlowModelVariable entity = new FlowModelVariable();
        BeanUtils.copyProperties(vo, entity);
        return flowModelVariableDAO.insertSelective(entity);
    }

    @Override
    public List<FlowModelVariableVo> getModelVariables(String deployId) {
        FlowModelVariableExample query = new FlowModelVariableExample();
        query.createCriteria().andDeployIdEqualTo(deployId);
        return flowModelVariableDAO.selectByExample(query).stream().map(this::convert).collect(Collectors.toList());
    }

    private FlowModelVariableVo convert(FlowModelVariable entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        FlowModelVariableVo result = new FlowModelVariableVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }
}
